package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCollect;
import com.cskaoyan.bean.MarketCollectExample;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.WxCollectVo;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author gatsby
 * @since 2022/10/14 11:44
 */
@Service
public class WxCollectServiceImpl implements WxCollectService {

    @Autowired
    MarketCollectMapper marketCollectMapper;

    @Autowired
    MarketUserMapper marketUserMapper;
    /**
     * 根据type分页查询全部收藏商品的业务逻辑
     *
     * @param basePageInfo
     * @param type
     * @return com.cskaoyan.bean.common.CommonData<com.cskaoyan.bean.vo.WxCollectVo>
     * @author gatsby
     * @since 2022/10/14 11:46
     */
    @Override
    public CommonData<WxCollectVo> queryCollectByTypeLimit(BasePageInfo basePageInfo, Short type) {
        //开启分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        List<WxCollectVo> wxCollectVoList = marketCollectMapper.selectByType(type);

        PageInfo<WxCollectVo> pageInfo = new PageInfo<>(wxCollectVoList);
        CommonData<WxCollectVo> commonData = CommonData.data(pageInfo);

        return commonData;
    }

    /**
     * 添加或者删除收藏品的业务逻辑
     *
     * @param map
     * @return void
     * @author gatsby
     * @since 2022/10/14 13:27
     */
    @Transactional
    @Override
    public void addOrDeleteCollect(Map map) {
        Integer type = (Integer)map.get("type");
        Integer valueId = (Integer)map.get("valueId");

        String s = type.toString();
        Byte aByte = Byte.valueOf(s);
        //先查出来用户id
        MarketUser marketUser = (MarketUser)SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer userId = marketUser.getId();
        //int userId=marketUserMapper.selectUserIdByUserName(username);
        //根据用户id和商品的id判断该商品是否已经存在数据库中
        MarketCollectExample example = new MarketCollectExample();
        example.setDistinct(true);
        MarketCollectExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(aByte);
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(example);
        //获取当前时间
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // String format = sdf.format(new Date());
        // Date now=null;
        // try {
        //     now = sdf.parse(format);
        // } catch (ParseException e) {
        //     e.printStackTrace();
        // }
        if(marketCollects.size()>0){
            //该商品已收藏过,那么此处的业务逻辑就是改变逻辑删除的状态码
            //即将deleted设为1，并且重置更新的时间
            MarketCollect marketCollect1 = marketCollects.get(0);
            marketCollect1.setDeleted(!marketCollect1.getDeleted());
            //重置更新时间

            //marketCollect1.setUpdateTime(now);
            marketCollect1.setUpdateTime(new Date());

            //更新
            marketCollectMapper.updateByPrimaryKeySelective(marketCollect1);

        }else{
            //该商品未收藏，直接新增
            MarketCollect marketCollect = new MarketCollect();
            marketCollect.setType( aByte);
            marketCollect.setValueId(valueId);
            //marketCollect.setAddTime(now);
            //marketCollect.setUpdateTime(now);
            marketCollect.setAddTime(new Date());
            marketCollect.setUpdateTime(new Date());
            marketCollect.setDeleted(false);
            marketCollect.setUserId(userId);
            //新增
            marketCollectMapper.insert(marketCollect);
        }




    }
}
